import {
  getTechnologyList,
  getProcessList,
  importUrl,
} from "@/api/process/mesProductProcessSteps";
import { getDict } from "@/api/common";
import excelUpload from "@/components/excel-upload/excelUpload";

const switchList = [
  {
    label: $i18n.t("common.yes"),
    value: "1",
  },
  {
    label: $i18n.t("common.no"),
    value: "0",
  },
];
const statusList = [
  {
    label: $i18n.t("mesProductProcessSteps.enable"),
    value: "1",
  },
  {
    label: $i18n.t("mesProductProcessSteps.deactivate"),
    value: "0",
  },
];
export const formDescDialogMixins = {
  data() {
    return {
      formDescDialog: {
        span: 24,
        isShowSubmitBtn: true,
        formDesc: {
          seriesName: {
            type: "input",
            label: $i18n.t("mesProductProcessSteps.series"), //系列
            layout: 12,
            required: true,
          },

          prodName: {
            type: "input",
            label: $i18n.t("mesProductProcessSteps.model"), //"型号",
            layout: 12,
          },

          technology: {
            type: "select",
            label: $i18n.t("mesProductProcessSteps.technology"), // "工艺",
            layout: 12,
            required: true,
            // prop: {
            //   value: "technologyCode",
            //   text: "technologyName",
            // },
            optionsLinkageFields: ["seriesName", "prodName"],
            options: async ({ seriesCode, prodCode }) => {
              if (!seriesCode) return [];
              const list = await getTechnologyList({ seriesCode, prodCode });
              let result = list.data.result || [];
              result.forEach((item) => {
                item.text = `${item.technologyName}(${item.versionNumber})`;
                item.value = `${item.technologyName}@${item.technologyCode}@${item.versionNumber}@${item.id}`;
              });
              return result;
            },
          },

          processCode: {
            type: "select",
            label: $i18n.t("mesProductProcessSteps.processes"), //"工序",processes
            layout: 12,
            required: true,
            prop: {
              value: "processCode",
              text: "processName",
            },
            optionsLinkageFields: ["technology"],

            options: async ({ technology }) => {
              if (!technology) return [];
              const [
                technologyName,
                technologyCode,
                versionNumber,
                id,
              ] = technology.split("@");
              const list = await getProcessList({
                technologyName,
                technologyCode,
                versionNumber,
              });
              let data = list.data.result || [];
              data.forEach((item) => {
                item.processCode = `${item.processName}@${item.processCode}`;
              });
              return data;
            },
          },
        },
      },
    };
  },
};
export const seriesSearchDialog = {
  inline: true,
  formDesc: {
    seriesCode: {
      type: "input",
      label: $i18n.t("mesProductProcessSteps.seriesCode"), //"系列编码",
      layout: 8,
    },
    seriesName: {
      type: "input",
      label: $i18n.t("mesProductProcessSteps.seriesName"), //"系列名称",
      layout: 8,
    },
  },
};
export const seriesColumn = [
  {
    type: "selection",
  },
  {
    type: "index",
  },
  {
    prop: "seriesCode",
    label: $i18n.t("mesProductProcessSteps.seriesCode"), // "系列编码",
  },
  {
    prop: "seriesName",
    label: $i18n.t("mesProductProcessSteps.seriesName"), //"系列名称",
  },
];

export const prodSearchDialog = {
  inline: true,
  formDesc: {
    goodsCode: {
      type: "input",
      label: $i18n.t("mesProductProcessSteps.goodsCode"), //"型号编码",
      layout: 8,
    },
    goodsName: {
      type: "input",
      label: $i18n.t("mesProductProcessSteps.goodsName"), //"型号名称",
      layout: 8,
    },
  },
};
export const prodSearchModelDialog = {
  inline: true,
  formDesc: {
    materialCode: {
      type: "input",
      label: $i18n.t("mesProductProcessSteps.materialCode"), //"型号编码",
      layout: 8,
    },
    materialName: {
      type: "input",
      label: $i18n.t("mesProductProcessSteps.materialName"), //"型号名称",
      layout: 8,
    },
  },
};
export const prodSearchModelDialogNew = {
  inline: true,
  formDesc: {
    goodsCode: {
      type: "input",
      label: $i18n.t("mesProductProcessSteps.materialCode"), //"型号编码",
      layout: 8,
    },
    goodsName: {
      type: "input",
      label: $i18n.t("mesProductProcessSteps.materialName"), //"型号名称",
      layout: 8,
    },
  },
};
export const regularSearchDialog = {
  inline: true,
  formDesc: {
    regularCode: {
      type: "input",
      label: $i18n.t("mesProductProcessSteps.regularCode"), //"正则编码",
      layout: 8,
    },
    regularName: {
      type: "input",
      label: $i18n.t("mesProductProcessSteps.regularName"), // "正则名称",
      layout: 8,
    },
  },
};
export const prodColumn = [
  {
    type: "selection",
  },
  {
    type: "index",
  },
  {
    prop: "seriesCode",
    label: $i18n.t("mesProductProcessSteps.seriesCode"), //"系列编码",
  },
  {
    prop: "seriesName",
    label: $i18n.t("mesProductProcessSteps.seriesName"), //"系列名称",
  },
  {
    prop: "goodsCode",
    label: $i18n.t("mesProductProcessSteps.goodsCode"), //"型号编码",
  },
  {
    prop: "goodsName",
    label: $i18n.t("mesProductProcessSteps.goodsName"), //"型号名称",
    showOverflowTooltip: true,
  },
];
export const materialColumn = [
  {
    type: "selection",
  },
  {
    type: "index",
  },
  {
    prop: "processCode",
    label: $i18n.t("mesProductProcessSteps.processCode"), //"工序编码",
  },
  {
    prop: "processName",
    label: $i18n.t("mesProductProcessSteps.processName"), //"工序名称",
  },
  {
    prop: "materialCode",
    label: $i18n.t("mesProductProcessSteps.materialCode"), //"物料编码",
  },
  {
    prop: "materialName",
    label: $i18n.t("mesProductProcessSteps.materialName"), //"物料名称",
    showOverflowTooltip: true,
  },
];
export const materialColumnNew = [
  {
    type: "selection",
  },
  {
    type: "index",
  },
  {
    prop: "seriesCode",
    label: $i18n.t("mesProductProcessSteps.seriesCode"), //"系列编码",
  },
  {
    prop: "seriesName",
    label: $i18n.t("mesProductProcessSteps.seriesName"), //"系列名称",
  },
  {
    prop: "goodsCode",
    label: $i18n.t("mesProductProcessSteps.materialCode"), //"物料编码",
  },
  {
    prop: "goodsName",
    label: $i18n.t("mesProductProcessSteps.materialName"), //"物料名称",
    showOverflowTooltip: true,
  },
];
export const regularColumn = [
  {
    type: "selection",
  },
  {
    type: "index",
  },
  {
    prop: "regularCode",
    label: $i18n.t("mesProductProcessSteps.regularCode"), //"正则编码",
  },
  {
    prop: "regularName",
    label: $i18n.t("mesProductProcessSteps.regularName"), // "正则名称",
  },
  {
    prop: "regularContent",
    label: $i18n.t("mesProductProcessSteps.regularContent"), //"正则表达式",
  },
];

export const formConfig = {
  inline: true,
  column: 3,
  formDesc: {
    step: {
      type: "input",
      label: $i18n.t("mesProductProcessSteps.step"), // "工步",
      layout: 8,
    },
    parameterMaterial: {
      type: "input",
      label: $i18n.t("mesProductProcessSteps.parameterMaterial"), //"参数/物料",
      layout: 8,
    },
    classify: {
      type: "select",
      label: $i18n.t("mesProductProcessSteps.classify"), //"类型",
      layout: 8,
      prop: {
        value: "value",
        text: "description",
      },
      options: async () => {
        const list = await getDict("step_type");
        return list.data.result || [];
      },
    },
    enableOrNot: {
      type: "select",
      label: $i18n.t("common.status"), // "状态",
      layout: 8,
      prop: {
        value: "value",
        text: "description",
      },
      options: async () => {
        const list = await getDict("active_status");
        return list.data.result || [];
      },
    },
  },
};
export const tableSteps = {
  data() {
    const _this = this;
    return {
      operateList: [
        {
          name: $i18n.t("common.export"), //"导出",
          type: "primary",
          icon: "el-icon-download",
          handleName: "export", //点击时返回的标识
        },
        {
          name: $i18n.t("common.add"), // "新增",
          type: "primary",
          icon: "el-icon-circle-plus-outline",
          disabled: () => {
            return !this.isgx();
          },
          handleName: "add", //点击时返回的标识
        },

        {
          // name: "导入",
          // type: "primary",
          // icon: "el-icon-upload2",
          // handleName: "import", //点击时返回的标识
          render: (h, scope) => {
            return (
              <excelUpload
                action={importUrl}
                on={{
                  handleUploadError: (err) => {
                    // 失败的回调
                    console.log(err);
                  },
                  handleUploadSuccess: (res) => {
                    _this.$emit('refreshTree')
                    _this.refresh();
                    //成功 的回调
                  },
                }}
              />
            );
          },
        },
        {
          name: $i18n.t("mesProductProcessSteps.preview"), // 预览,
          type: "success",
          icon: "el-icon-view",
          handleName: "preview", //点击时返回的标识
          disabled: () => {
            return !this.isgx();
          },
        },
        {
          name: $i18n.t("common.save"), //"保存",
          type: "warning",
          icon: "el-icon-check",
          handleName: "save", //点击时返回的标识
          disabled: () => {
            return !this.isgx();
          },
        },

        {
          name: $i18n.t("common.downloadTemplate"), //"模板下载",
          type: "success",
          icon: "el-icon-download",
          handleName: "download", //点击时返回的标识
        },
      ],
      // 行末按钮配置
      rowConfig: [
        {
          name: $i18n.t("common.edit"), //"编辑",
          type: "primary",
          handleName: "edit", //点击时返回的标识
          disabled: () => {
            return !this.isgx();
          },
        },
        {
          name: $i18n.t("mesProductProcessSteps.remove"), // "移除",
          type: "danger",
          handleName: "delete", //点击时返回的标识
          disabled: () => {
            return !this.isgx();
          },
        },
        {
          name: $i18n.t("mesProductProcessSteps.copy"), //"复制",
          type: "primary",
          handleName: "copy", //点击时返回的标识
          disabled: () => {
            return !this.isgx();
          },
        },
        // {
        //   name: $i18n.t("mesProductProcessSteps.up"), //"上移",
        //   type: "success",
        //   handleName: "up", //点击时返回的标识
        //   disabled: () => {
        //     return !this.isgx();
        //   },
        // },
        // {
        //   name: $i18n.t("mesProductProcessSteps.down"), //"下移"
        //   type: "success",
        //   handleName: "down", //点击时返回的标识
        //   disabled: () => {
        //     return !this.isgx();
        //   },
        // },
      ],
      stepsColumn: [
        {
          type: "index",
          label: $i18n.t("common.order"), //"序号",
          width: 80,
        },
        // {
        //   prop: "orderType",
        //   label: $i18n.t("mesProductProcessSteps.orderType"), //"顺序类型", //下拉
        //   minWidth: 180,
        //   render: (h, scope) => {
        //     return this.getSelectDOm(h, scope, "orderType", this.sequenceList);
        //   },
        // },
        {
          prop: "parallelNumber",
          label: $i18n.t("mesProductProcessSteps.sort"), //, //"并序编号",
          minWidth: 210,
          render: (h, scope) => {
            const { orderType } = scope.row;
            let dicInput = ["1"]; //并序
            if (this.isAddOrEdit(scope.row)) {
              return this.getInputNumberNew(h, scope, "parallelNumber");
            }
            return <div>{scope.row.parallelNumber}</div>;
          },
        },
        {
          prop: "step",
          label: $i18n.t("mesProductProcessSteps.step"), // "工步",
          minWidth: 160,
          showOverflowTooltip: true,
          render: (h, scope) => {
            return this.getInputDom(h, scope, "step");
          },
        },
        {
          prop: "classify",
          label: $i18n.t("mesProductProcessSteps.classify"), //"类型",
          minWidth: 130,
          render: (h, scope) => {
            return this.getSelectDOm(h, scope, "classify", this.typeList);
          },
        },
        {
          prop: "parameterMaterial",
          label: $i18n.t("mesProductProcessSteps.parameterMaterialType"), //"参数类型/物料代码/投料批次号",
          width: 330,
          render: (h, scope) => {
            const { classify } = scope.row;
            let dicInput = ["1", "2", "3", "9", "10", "12", "13"]; //计量、计数、计时
            let dicSearch = ["7", "8"]; //物料，扫描
            // let dicDisabled = ["4", "5", "6", "9", "10", "11"]; //描述、图片、附件、单选、多选、主关件绑定
            if (dicInput.includes(classify) && this.isAddOrEdit(scope.row)) {
              return this.getInput(h, scope, "parameterMaterial");
            }
            if (dicSearch.includes(classify) && this.isAddOrEdit(scope.row)) {
              return this.getSearchInput(h, scope, "parameterMaterial");
            }
            return <div>{scope.row.parameterMaterial}</div>;
          },
        },
        {
          prop: "referenceValue",
          label: $i18n.t("mesProductProcessSteps.referenceValue"), //"参考值",
          minWidth: 220,
          render: (h, scope) => {
            const { classify } = scope.row;
            let dicInput = ["1", "2", "3", "7", "12", "13"]; //计量、计数、计时
            let dicSearch = ["9", "10"]; //单选、多选
            if (dicInput.includes(classify) && this.isAddOrEdit(scope.row)) {
              return this.getInputNumber(h, scope, "referenceValue");
            }
            if (dicSearch.includes(classify) && this.isAddOrEdit(scope.row)) {
              return this.getInputDom(h, scope, "referenceValue");
            }

            return <div>{scope.row.referenceValue}</div>;
          },
        },
        {
          prop: "upperLimitValue",
          label: $i18n.t("mesProductProcessSteps.upperLimitValue"), //"上限",
          minWidth: 180,
          render: (h, scope) => {
            const { classify } = scope.row;
            let dicInput = ["1", "2", "3", "7", "12", "13"]; //计量、计数、计时、数采
            if (dicInput.includes(classify) && this.isAddOrEdit(scope.row)) {
              return this.getInputNumber(h, scope, "upperLimitValue");
            }

            return <div>{scope.row.upperLimitValue}</div>;
          },
        },
        {
          prop: "lowerLimitingValue",
          label: $i18n.t("mesProductProcessSteps.lowerLimitingValue"), //"下限",
          minWidth: 180,
          render: (h, scope) => {
            const { classify } = scope.row;
            let dicInput = ["1", "2", "3", "7", "12", "13"]; //计量、计数、计时、数采
            if (dicInput.includes(classify) && this.isAddOrEdit(scope.row)) {
              return this.getInputNumber(h, scope, "lowerLimitingValue");
            }
            return <div>{scope.row.lowerLimitingValue}</div>;
          },
        },
        {
          prop: "unitCode",
          label: $i18n.t("mesProductProcessSteps.unitCode"), //"计量单位", //字典
          minWidth: 130,
          render: (h, scope) => {
            const { classify } = scope.row;
            let dicDisabled = ["4", "5", "6", "8", "9", "10", "11"]; //描述、图片、附件、单选、多选、主关件绑定

            if (
              !dicDisabled.includes(classify) &&
              this.isAddOrEdit(scope.row)
            ) {
              return this.getSelect(h, scope, "unitCode", this.unitList);
            }
            return <div>{scope.row.unit}</div>;
          },
        },
        {
          prop: "selectMaterialOrNot",
          label: $i18n.t("mesProductProcessSteps.selectMaterialOrNot"), //"是否选择物料", //1是0否
          width: 130,
          render: (h, scope) => {
            return this.getSelectDOm(
              h,
              scope,
              "selectMaterialOrNot",
              switchList
            );
          },
        },
        {
          prop: "correctOptions",
          label: $i18n.t("mesProductProcessSteps.correctOptions"), //"正确选项", //input
          minWidth: 220,
          render: (h, scope) => {
            return this.getInputDom(h, scope, "correctOptions");
          },
        },
        {
          prop: "warningMessage",
          label: $i18n.t("mesProductProcessSteps.warningMessage"), //"说明",
          minWidth: 200,
          render: (h, scope) => {
            return this.getInputDom(h, scope, "warningMessage");
          },
        },
        {
          prop: "isMandatory",
          label: $i18n.t("mesProductProcessSteps.isMandatory"), //"强制",
          minWidth: 100,
          render: (h, scope) => {
            return this.getSelectDOm(h, scope, "isMandatory", switchList);
          },
        },
        {
          prop: "isCopy",
          label: $i18n.t("mesProductProcessSteps.copy"), //"复制",
          minWidth: 100,
          render: (h, scope) => {
            return this.getSelectDOm(h, scope, "isCopy", switchList);
          },
        },
        {
          prop: "isRepeat",
          label: $i18n.t("mesProductProcessSteps.repeat"), // "重复",
          minWidth: 100,
          render: (h, scope) => {
            return this.getSelectDOm(h, scope, "isRepeat", switchList);
          },
        },
        {
          prop: "seriesName",
          label: $i18n.t("mesProductProcessSteps.series"), //"系列",
          minWidth: 120,
          showOverflowTooltip: true,
        },
        {
          prop: "prodName",
          label: $i18n.t("mesProductProcessSteps.model"), //"型号",
          minWidth: 160,
          showOverflowTooltip: true,
        },
        {
          prop: "versionNumber",
          label: $i18n.t("mesProductProcessSteps.versionNumber"), //"工艺版本",
          minWidth: 130,
        },
        {
          prop: "technologyName",
          label: $i18n.t("mesProductProcessSteps.technology"), // "工艺",
          minWidth: 160,
          showOverflowTooltip: true,
        },
        {
          prop: "processName",
          label: $i18n.t("mesProductProcessSteps.processes"), //"工序",
          minWidth: 160,
          showOverflowTooltip: true,
        },
        {
          prop: "enableOrNot",
          label: $i18n.t("common.status"), //status "状态",
          minWidth: 100,
          render: (h, scope) => {
            return this.getSelectDOm(h, scope, "enableOrNot", statusList);
          },
        },

        {
          prop: "operate",
          label: $i18n.t("common.operation"),
          width: 160,
          fixed: "right",
          render: (h, scope) => {
            return (
              <ele-row-operation
                scope={scope}
                rowConfig={this.rowConfig}
                onRowOperate={this.rowOperate}
              />
            );
          },
        },
      ],
    };
  },
};
